home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / lin_alg.lha / lin_alg / vector.cc < prev    next >
C/C++ Source or Header  |  1993-08-08  |  3KB  |  117 lines

  1. // This may look like C code, but it is really -*- C++ -*-
  2. /*
  3.  ************************************************************************
  4.  *
  5.  *            Linear Algebra Package
  6.  *
  7.  *        Basic Linear Algebra operations, level 1 & 2
  8.  *             concerning specifically vectors
  9.  *
  10.  * The present file is concerned with the operations which either
  11.  *    - specifically defined for vectors, such as norms
  12.  *     - some BLAS 1 & 2 operations that can be implemented more 
  13.  *      efficiently than general operations on n*1 matrices
  14.  *
  15.  ************************************************************************
  16.  */
  17.  
  18. #include "LinAlg.h"
  19. #include <math.h>
  20. #include <builtin.h>
  21.  
  22. #pragma implementation
  23.  
  24. /*
  25.  *------------------------------------------------------------------------
  26.  *               Specific vector constructors
  27.  */
  28.  
  29. #include <stdarg.h>
  30.             // Make a vector and assign initial values
  31.             // Argument list should contain DOUBLE values
  32.             // to assign to vector elements. The list must
  33.             // be terminated by the string "END"
  34.             // Example: Vector foo(1,3,0.0,1.0,1.5,"END");
  35. Vector::Vector(const short lwb, const short upb, double iv1, ... )
  36.   : Matrix(lwb,upb,1,1)
  37. {
  38.   va_list args;
  39.   va_start(args,iv1);            // Init 'args' to the beginning of
  40.                     // the variable length list of args
  41.   register int i;
  42.   (*this)(lwb) = iv1;
  43.   for(i=lwb+1; i<=upb; i++)
  44.     (*this)(i) = (double)va_arg(args,double);
  45.  
  46.   assure( strcmp((char *)va_arg(args,char *),"END") == 0,
  47.      "Vector: argument list must be terminated by \"END\" ");
  48. }
  49.  
  50. /*
  51.  *------------------------------------------------------------------------
  52.  *            Computing Vector norms
  53.  */
  54.  
  55.                 // Compute the 1-norm of the vector
  56.                 // SUM{ |v[i]| }
  57. double Vector::norm_1(void) const
  58. {
  59.   is_valid();
  60.  
  61.   register double norm = 0;
  62.   register REAL * vp;
  63.  
  64.   for(vp = elements; vp < elements + nelems; )
  65.     norm += ::abs(*vp++);
  66.  
  67.   return norm;
  68. }
  69.  
  70.                 // Compute the square of the 2-norm
  71.                 // SUM{ v[i]^2 }
  72. double Vector::norm_2_sqr(void) const
  73. {
  74.   is_valid();
  75.  
  76.   register double norm = 0;
  77.   register REAL * vp;
  78.  
  79.   for(vp = elements; vp < elements + nelems; )
  80.     norm += ::sqr(*vp++);
  81.  
  82.   return norm;
  83. }
  84.                 // Compute the infinity-norm of the vector
  85.                 // MAX{ |v[i]| }
  86. double Vector::norm_inf(void) const
  87. {
  88.   is_valid();
  89.  
  90.   register double norm = 0;
  91.   register REAL * vp;
  92.  
  93.   for(vp = elements; vp < elements + nelems; )
  94.     norm = ::max(norm,::abs(*vp++));
  95.  
  96.   return norm;
  97. }
  98.  
  99. /*
  100.  *------------------------------------------------------------------------
  101.  *        Multiplications specifically defined for vectors
  102.  */
  103.  
  104.                 // Compute the scalar product
  105. double operator * (const Vector& v1, const Vector& v2)
  106. {
  107.   are_compatible(v1,v2);
  108.   register REAL * v1p = v1.elements;
  109.   register REAL * v2p = v2.elements;
  110.   register double sum = 0;
  111.  
  112.   while( v1p < v1.elements + v1.nelems )
  113.     sum += *v1p++ * *v2p++;
  114.  
  115.   return sum;
  116. }
  117.